package com.travel.service;

import com.travel.constants.Constants;
import com.travel.constants.ShiroConstants;
import com.travel.constants.UserConstants;
import com.travel.exception.user.*;
import com.travel.model.SysUser;
import com.travel.model.WebUser;
import com.travel.utils.*;
import org.apache.shiro.authc.credential.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SysLoginService
{
    @Autowired
    private WebPasswordService passwordService;


    @Autowired
    private IWebUserService userService;

    /**
     * 登录
     */
    public WebUser login(String email, String password)
    {

        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password))
        {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(email, Constants.LOGIN_FAIL, MessageUtils.message("not.null")));
            throw new UserNotExistsException();
        }

        // 查询用户信息
        WebUser user = userService.selectUserByEmail(email);
        passwordService.validate(user, password);
        setRolePermission(user);
        return user;
    }

    /**
     private boolean maybeEmail(String username)
     {
     if (!username.matches(UserConstants.EMAIL_PATTERN))
     {
     return false;
     }
     return true;
     }

     private boolean maybeMobilePhoneNumber(String username)
     {
     if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
     {
     return false;
     }
     return true;
     }
     */

    /**
     * 设置角色权限
     *
     * @param user 用户信息
     */
    public void setRolePermission(WebUser user)
    {
//        List<SysRole> roles = user.getRoles();
//        if (!roles.isEmpty() && roles.size() > 1)
//        {
//            // 多角色设置permissions属性，以便数据权限匹配权限
//            for (SysRole role : roles)
//            {
//                Set<String> rolePerms = menuService.selectPermsByRoleId(role.getRoleId());
//                role.setPermissions(rolePerms);
//            }
//        }
    }



}
